<template>
  <div>
    <el-dialog
      v-model="props.isShow"
      :title="props.isAdd ? '添加菜单' : '编辑菜单'"
      center
      :before-close="cancel"
    >
      <el-form :model="menuForm" :rules="rules" ref="menuRef">
        <el-form-item label="菜单名称" prop="title" :label-width="formLabelWidth">
          <el-input v-model="menuForm.title" autocomplete="off" />
        </el-form-item>
        <el-form-item label="上级菜单" prop="pid" :label-width="formLabelWidth">
          <el-select v-model="menuForm.pid" placeholder="Please select a zone">
            <el-option label="顶级菜单" :value="0" />
            <el-option
              v-for="item in $store.getters['menu/getMenu']"
              :label="item.title"
              :value="item.id"
              :key="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="菜单类型" :label-width="formLabelWidth">
          <el-radio-group v-model="menuForm.type">
            <el-radio :label="1" size="large">目录</el-radio>
            <el-radio :label="2" size="large">菜单</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          label="菜单图标"
          :label-width="formLabelWidth"
          v-if="menuForm.type == 1"
        >
          <el-input v-model="menuForm.icon" autocomplete="off" />
        </el-form-item>
        <el-form-item label="菜单地址" :label-width="formLabelWidth" v-else>
          <el-input v-model="menuForm.url" autocomplete="off" />
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="menuForm.status"
            class="ml-2"
            style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"
            :active-value="1"
            :inactive-value="2"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="cancel(menuRef)">取消</el-button>
          <el-button type="primary" @click="confirm(menuRef)"> 确定 </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { defineProps, defineEmits, ref, reactive, onMounted } from "vue";
import type { FormInstance, FormRules } from "element-plus";
import { ElMessage } from "element-plus";
import { menuadd, menuinfo, menuedit } from "../../request/api";
import { useStore } from "vuex";
let store = useStore();

// 单选
const radio = ref(3);
// 开关选择器
const value2 = ref(true);

let props = defineProps({
  isShow: Boolean,
  isAdd: Boolean,
});

const dialogTableVisible = ref(false);
const dialogFormVisible = ref(false);
const formLabelWidth = "140px";

// 双向数据绑定的数据
let menuForm = ref({
  pid: 0, // 上级分类编号  注意：顶级菜单-0number
  title: "", //菜单名称string
  icon: "", //图标string
  type: 1, //类型 1目录2菜单number
  url: "", //菜单地址string
  status: 1, //状态   1正常2禁用   number类型number
});

//定义 rules规则
let rules = reactive<FormRules>({
  title: [
    { required: true, message: "请输入菜单名称", trigger: "blur" },
    { min: 2, max: 6, message: "Length should be 2 to 6", trigger: "blur" },
  ],
  pid: [
    {
      required: true,
      message: "请选择上级菜单",
      trigger: "change",
    },
  ],
});

// 调用子传父
let emit = defineEmits(["cancel"]);

// 获取表单信息
let menuRef = ref<FormInstance>();

// 取消
let cancel = (formEl?: FormInstance | undefined) => {
  emit("cancel", false);
  menuForm.value = {
    pid: 0, // 上级分类编号  注意：顶级菜单-0number
    title: "", //菜单名称string
    icon: "", //图标string
    type: 1, //类型 1目录2菜单number
    url: "", //菜单地址string
    status: 1, //状态   1正常2禁用   number类型number
  };
  // 清楚规则验证
  menuRef.value.resetFields()
};

onMounted(() => {
  store.dispatch("menu/getMenuActions");
});

// 确定
let confirm = async (formEl?: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((val) => {
    if (val) {
      console.log(menuForm.value);
      if (props.isAdd) {
        menuadd(menuForm.value).then((res) => {
          if (res.code == 200) {
            ElMessage({
              showClose: true,
              message: res.msg,
              type: "success",
            });
            cancel();
          }
          store.dispatch("menu/getMenuActions");
        });
      } else {
        menuedit(menuForm.value).then((res) => {
          // console.log(res);
          if (res.code == 200) {
            ElMessage({
              showClose: true,
              message: res.msg,
              type: "success",
            });
            cancel();
          } else {
            ElMessage({
              showClose: true,
              message: res.msg,
              type: "error",
            });
          }
          store.dispatch("menu/getMenuActions");
        });
      }
    }
  });
};

// 编辑
let lookup = async (id: any) => {
  let result: any = await menuinfo({ id });
  // console.log(result);
  if (result.code == 200) {
    menuForm.value = result.list;
    // 修改的时候需要id
    menuForm.value.id = id;
  }
};

// 可以通过defineExpose 编译器宏来显示指定在 <script setup> 组件中要暴露出去的属性
defineExpose({
  lookup,
});
</script>

<style lang="less" scoped>
.el-button--text {
  margin-right: 15px;
}
.el-select {
  width: 300px;
}
.el-input {
  width: 300px;
}
.dialog-footer button:first-child {
  margin-right: 10px;
}
</style>
